from django.db import models
import datetime
from django.utils import timezone

# 数据库模型 投票应用
# 需要两个模型 问题-Question  选项-Choice
'''Question-问题描述及发布时间
    Choice-选项描述及当前票数'''


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        now = timezone.now()
        # return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
        # 修复日期bug
        return now - datetime.timedelta(days=1) <= self.pub_date <= now


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)  # 关系，每个Choice对象都关联到一个Question对象
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text
# Create your models here.
